package cn.sc.summer.redis.redisson.strategy.impl;


import cn.sc.summer.redis.redisson.property.RedissonProperties;
import cn.sc.summer.constant.redis.RedisConstant;
import cn.sc.summer.redis.redisson.strategy.RedissonConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.config.Config;
import org.springframework.stereotype.Service;

/**
 * @author a-xin
 * @Description: 单机部署Redisson配置
 * @date 2019/6/19 下午10:04
 */
@Slf4j
@Service(value = "StandaloneConfigImpl")
public class StandaloneConfigImpl implements RedissonConfigService {

    @Override
    public Config createRedissonConfig(RedissonProperties redissonProperties) {
        Config config = new Config();
        try {
            String address = redissonProperties.getHost() + ":" + redissonProperties.getPort();
            String password = redissonProperties.getPassword();
            int database = redissonProperties.getDatabase();
            String redisAddr = RedisConstant.REDIS_CONNECTION_PREFIX + address;
            config.useSingleServer().setAddress(redisAddr);
            config.useSingleServer().setDatabase(database);
            //密码可以为空
            if (StringUtils.isNotBlank(password)) {
                config.useSingleServer().setPassword(password);
            }
            log.info("==> Initialize the [Stand-alone Deployment] mode Config, redisAddress:" + address);
        } catch (Exception e) {
            log.error("==> Stand-alone Redisson init error", e);
        }
        return config;
    }
}
